package com.module.circle.ui.changeIntroduction

import android.text.Editable
import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.common.app.base.AppPictureSelectionActivity
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.KeyEvents
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.setOnClickListener
import com.common.base.app.extras.showToast
import com.common.base.app.extras.yes
import com.common.base.utils.SimpleTextWatcher
import com.common.base.utils.StatusBarUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.module.circle.R
import com.module.circle.databinding.CircleActivityChangeIntroductionBinding

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/07/23
 * <p>
 * 描述：修改个人简介界面
 * <p>
 * 修订历史：
 */
class ChangeMyIntroductionActivity : AppPictureSelectionActivity<CircleActivityChangeIntroductionBinding, ChangeIntroductionViewModel>(), View.OnClickListener  {

    override fun getViewModel() = ChangeIntroductionViewModel::class.java
    override fun getViewContentBinding() = CircleActivityChangeIntroductionBinding.inflate(layoutInflater)
    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    @Autowired(name = KeyBundle.BUNDLE_KEY)
    @JvmField
    var mValue: String? = null

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)
        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle(resources.getString(R.string.circle_ac_change_introduction_title))
        headTitleUtils.setLineStyle(10)

    }

    override fun initEvents() {
        super.initEvents()

        mViewContentBinding.edtIntroduction.addTextChangedListener(object : SimpleTextWatcher() {
            override fun afterTextChanged(s: Editable?) {
                mViewContentBinding.tvIntroductionNum.text = "${s?.length.toString()}/40"
            }
        })

        mViewContentBinding.tvSaveBtn.setOnClickListener(onClick = this)

    }

    override fun lazyInitData() {
        super.lazyInitData()

        mValue?.apply {
            mViewContentBinding.edtIntroduction.setText(this)
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            mViewContentBinding.tvSaveBtn->{
                modifyUsrInfo(mViewContentBinding.edtIntroduction.text.toString().trim())
            }
        }
    }

    private fun modifyUsrInfo(mDescription: String?) {
        showDialogLoading()
        mViewModel.modifyUsrInfo(description=mDescription).observe(this) {
            dismissLoadingDialog()
            it.isSuccess().yes {
                LiveEventBus.get(KeyEvents.KEY_REQUEST_USER_INFO_DATA).post(this)
                if (!TextUtils.isEmpty(mViewContentBinding.edtIntroduction.text.toString().trim())) {
                    intent.putExtra("note", mViewContentBinding.edtIntroduction.text.toString().trim())
                }
                setResult(RESULT_OK, intent)
                finish()
            }.otherwise {
                showToast(it.Message)
            }
        }
    }
}